package com.iwhalecloud.uncc.dao.impl;

import com.iwhalecloud.uncc.common.ArchiverConstant;
import com.iwhalecloud.uncc.dao.ArchiverDao;
import com.iwhalecloud.uncc.db.DatasourceContextHolder;
import com.iwhalecloud.uncc.model.InsCollectTaskBatch;
import com.iwhalecloud.uncc.utils.BatchUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * @author zq
 */
@Repository("insCollectTaskBatchArchiverDao")
public class InsCollectTaskBatchArchiverDaoImpl implements ArchiverDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public Object query(Object o) {
        String sql = "SELECT * FROM INS_COLLECT_TASK_BATCH WHERE BATCH_NO = ? AND ARCHIVER_FLAG = " + ArchiverConstant.ONLINE;
        List<InsCollectTaskBatch> list = jdbcTemplate.query(sql, new Object[]{o} , new BeanPropertyRowMapper<>(InsCollectTaskBatch.class));
        if (!list.isEmpty()) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public List queryList(Object o) {

        return null;
    }

    @Override
    public List batchQuery(List list) {
        StringBuilder sql = new StringBuilder("SELECT");
        sql.append(" `BATCH_NO`, `STRATEGY_ID`, `BATCH_STATE`, `TASK_COUNT`, `START_TIME`, `PARTITION_CODE`");
        sql.append(" FROM INS_COLLECT_TASK_BATCH WHERE BATCH_NO in (:i)");
        sql.append(" AND PARTITION_CODE = "+ DatasourceContextHolder.getDs());
        List<InsCollectTaskBatch> res = BatchUtil.batchQueryBeans(sql,list,new RowMapper() {
            @Override
            public Object mapRow(ResultSet resultSet, int i) throws SQLException {
                InsCollectTaskBatch insCollectTaskBatch = new InsCollectTaskBatch();
                insCollectTaskBatch.setBatchNo(resultSet.getString("BATCH_NO"));
                insCollectTaskBatch.setStrategyId(resultSet.getLong("STRATEGY_ID"));
                insCollectTaskBatch.setBatchState(resultSet.getInt("BATCH_STATE"));
                insCollectTaskBatch.setTaskCount(resultSet.getInt("TASK_COUNT"));
                insCollectTaskBatch.setStartTime(resultSet.getTimestamp("START_TIME"));
                insCollectTaskBatch.setPartitionCode(resultSet.getString("PARTITION_CODE"));
                return insCollectTaskBatch;
            }
        });
        return res;
    }

    @Override
    public void insert(Object o) {
        InsCollectTaskBatch insCollectTaskBatch = (InsCollectTaskBatch) o;
        String sql = " insert into `INS_COLLECT_TASK_BATCH` (`BATCH_NO`, `STRATEGY_ID`, `BATCH_STATE`, `TASK_COUNT`, `START_TIME`, `PARTITION_CODE`,`ARCHIVER_TIME`)  " +
                " VALUES (?,?,?,?,?,?,?) ";
        jdbcTemplate.update(sql, new Object[]{insCollectTaskBatch.getBatchNo(), insCollectTaskBatch.getStrategyId(), insCollectTaskBatch.getBatchState(),
                insCollectTaskBatch.getTaskCount(),  insCollectTaskBatch.getStartTime(), insCollectTaskBatch.getPartitionCode(),insCollectTaskBatch.getArchiverTime()});
    }

    @Override
    public void batchInsert(List list) {
        StringBuilder sql = new StringBuilder("INSERT INTO `ins_collect_task_batch`");
        sql.append(" (`BATCH_NO`, `STRATEGY_ID`, `BATCH_STATE`, `TASK_COUNT`, `START_TIME`, `PARTITION_CODE`, `ARCHIVER_TIME`)");
        sql.append(" VALUES (:batchNo,:strategyId,:batchState,:taskCount,:startTime,:partitionCode,:archiverTime)");
        BatchUtil.batchUpdateBeans(sql,list);
    }

    @Override
    public void update(int flag, Object o) {
        String sql = "UPDATE INS_COLLECT_TASK_BATCH SET ARCHIVER_FLAG = " + flag + " WHERE BATCH_NO = ?";
        jdbcTemplate.update(sql, new Object[]{o});
    }

    @Override
    public void batchUpdate(int flag, List list) {
        StringBuilder sql = new StringBuilder("UPDATE INS_COLLECT_TASK_BATCH SET ARCHIVER_FLAG = " + flag + " WHERE BATCH_NO = :batchNo");
        sql.append(" AND PARTITION_CODE = "+ DatasourceContextHolder.getDs());
        BatchUtil.batchUpdateBeans(sql,list);
    }

    @Override
    public void delete(Object o) {
        String sql = "DELETE FROM INS_COLLECT_TASK_BATCH WHERE BATCH_NO = ?";
        jdbcTemplate.update(sql, new Object[]{o});
    }

    @Override
    public void batchDelete(List list) {
        StringBuilder sql = new StringBuilder("DELETE FROM INS_COLLECT_TASK_BATCH WHERE BATCH_NO = :batchNo");
        sql.append(" AND PARTITION_CODE = "+ DatasourceContextHolder.getDs());
        BatchUtil.batchUpdateBeans(sql,list);
    }
}
